﻿//-----------------------------------------------------------------------
// <copyright file="FilterViewModel.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.PrivateMessages
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows.Input;

    using ChatBox.DomainObjects.Settings;
    using ChatBox.DomainObjects.XmlFeedObjects;
    using ChatBox.UI.MessageFilters;
    using ChatBox.UI.ViewModels;
 
    /// <summary>
    /// Private messages filter view model
    /// </summary>
    public class FilterViewModel : BaseViewModel
    {
        #region data

        /// <summary>
        /// Command to read settings
        /// </summary>
        private readonly ICommand readSettingsCommand;

        /// <summary>
        /// Command to write settings
        /// </summary>
        private readonly ICommand writeSettingsCommand;

        /// <summary>
        /// the command to generate the filter
        /// </summary>
        private readonly ICommand generateFilterCommand;

        /// <summary>
        /// the command to clear the filter
        /// </summary>
        private readonly ICommand clearFilterCommand;

        /// <summary>
        /// the command to set up selection lists
        /// </summary>
        private readonly ICommand setSelectionListsCommand;

        /// <summary>
        /// Groups to show in the group filter dropdown
        /// </summary>
        private readonly ObservableCollection<GroupFilterSelection> groups = new ObservableCollection<GroupFilterSelection>();

        /// <summary>
        /// Senders to show in the "from" filter dropdown
        /// </summary>
        private readonly ObservableCollection<Link> senders = new ObservableCollection<Link>();

        /// <summary>
        /// The selected group
        /// </summary>
        private GroupFilterSelection selectedGroup;

        /// <summary>
        /// The selected sender
        /// </summary>
        private Link selectedSender;

        /// <summary>
        /// The group that was selected in the dropdown
        /// </summary>
        private int selectedGroupIndex;

        /// <summary>
        /// The sender that was selected in the dropdown
        /// </summary>
        private int selectedSenderIndex;

        /// <summary>
        /// The filter text
        /// </summary>
        private string text;

        /// <summary>
        /// The maximum datetime
        /// </summary>
        private DateTime? maxDate;

        /// <summary>
        /// The number of messages to show
        /// </summary>
        private int messageCount;

        /// <summary>
        /// The min number of messages to show
        /// </summary>
        private int minMessageCount;

        /// <summary>
        /// The max number of messages to show
        /// </summary>
        private int maxMessageCount;

        /// <summary>
        /// Are the filter controls enabled
        /// </summary>
        private bool filterEnabled;

        #endregion

        /// <summary>
        /// Initializes a new instance of the FilterViewModel class
        /// </summary>
        /// <param name="readSettingsCommand">the command to read settings</param>
        /// <param name="writeSettingsCommand">the command to write settings</param>
        /// <param name="generateFilterCommand">the command to generate a filter</param>
        /// <param name="clearFilterCommand">the command to clear filters</param>
        /// <param name="setSelectionListsCommand">the command to setup lists of data</param>
        public FilterViewModel(
            FilterReadSettingsCommand readSettingsCommand,
            FilterWriteSettingsCommand writeSettingsCommand,
            GenerateFilterCommand generateFilterCommand, 
            ClearFilterCommand clearFilterCommand,
            FilterSetSelectionListsCommand setSelectionListsCommand)
        {
            this.MinMessageCount = UISettingsDataHelper.MinMaxPrivateMessagesShown;
            this.MaxMessageCount = UISettingsDataHelper.MaxMaxPrivateMessagesShown;

            this.readSettingsCommand = readSettingsCommand;
            this.writeSettingsCommand = writeSettingsCommand;
            this.generateFilterCommand = generateFilterCommand;
            this.clearFilterCommand = clearFilterCommand;
            this.setSelectionListsCommand = setSelectionListsCommand;
        }

        #region dependency properties

        /// <summary>
        /// Gets the groups to show in the group filter dropdown
        /// </summary>
        public ObservableCollection<GroupFilterSelection> Groups
        {
            get
            {
                return this.groups;
            }
        }

        /// <summary>
        ///  Gets the senders to show in the "from" filter dropdown
        /// </summary>
        public ObservableCollection<Link> Senders
        {
            get
            {
                return this.senders;
            }
        }

        /// <summary>
        /// Gets or sets the group that was selected in the dropdown
        /// </summary>
        public int SelectedGroupIndex
        {
            get
            {
                return this.selectedGroupIndex;
            }

            set
            {
                if (this.selectedGroupIndex != value)
                {
                    this.selectedGroupIndex = value;
                    this.OnPropertyChanged("SelectedGroupIndex");
                }
            }
        }

        /// <summary>
        /// Gets or sets the sender that was selected in the dropdown
        /// </summary>
        public int SelectedSenderIndex
        {
            get
            {
                return this.selectedSenderIndex;
            }

            set
            {
                if (this.selectedSenderIndex != value)
                {
                    this.selectedSenderIndex = value;
                    this.OnPropertyChanged("SelectedSenderIndex");
                }
            }
        }

        /// <summary>
        /// Gets or sets the filter text
        /// </summary>
        public string Text
        {
            get
            {
                return this.text;
            }

            set
            {
                if (this.text != value)
                {
                    this.text = value;
                    this.OnPropertyChanged("Text");
                }
            }
        }

        /// <summary>
        /// Gets or sets the maximum datetime
        /// </summary>
        public DateTime? MaxDate
        {
            get
            {
                return this.maxDate;
            }

            set
            {
                if (this.maxDate != value)
                {
                    this.maxDate = value;
                    this.OnPropertyChanged("MaxDate");
                }
            }
        }

        /// <summary>
        /// Gets or sets the maximum messages to show
        /// </summary>
        public int MessageCount
        {
            get
            {
                return this.messageCount;
            }
            
            set
            {
                if (this.messageCount != value)
                {
                    this.messageCount = value;
                    this.OnPropertyChanged("MessageCount");
                }
            }
        }

        /// <summary>
        /// Gets or sets the minimum number of messages to show
        /// </summary>
        public int MinMessageCount
        {
            get
            {
                return this.minMessageCount;
            }

            set
            {
                if (this.minMessageCount != value)
                {
                    this.minMessageCount = value;
                    this.OnPropertyChanged("MinMessageCount");
                }
            }
        }

        /// <summary>
        /// Gets or sets the maximum messages to show
        /// </summary>
        public int MaxMessageCount
        {
            get
            {
                return this.maxMessageCount;
            }

            set
            {
                if (this.maxMessageCount != value)
                {
                    this.maxMessageCount = value;
                    this.OnPropertyChanged("MaxMessageCount");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the filter controls are enabled
        /// </summary>
        public bool FilterEnabled
        {
            get
            {
                return this.filterEnabled;
            }

            set
            {
                if (this.filterEnabled != value)
                {
                    this.filterEnabled = value;
                    this.OnPropertyChanged("FilterEnabled");
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected group
        /// </summary>
        public GroupFilterSelection SelectedGroup
        {
            get
            {
                return this.selectedGroup;
            }

            set
            {
                if (this.selectedGroup != value)
                {
                    this.selectedGroup = value;
                    this.OnPropertyChanged("SelectedGroup");
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected sender
        /// </summary>
        public Link SelectedSender
        {
            get
            {
                return this.selectedSender;
            }

            set
            {
                if (this.selectedSender != value)
                {
                    this.selectedSender = value;
                    this.OnPropertyChanged("SelectedSender");
                }
            }
        }

        #endregion

        #region other properties

        /// <summary>
        /// Gets or sets a value indicating whether the the filtered list has exceeded the max count and been cut
        /// </summary>
        public bool FilteredListIsTruncatedToLatest { get; set; }

        /// <summary>
        /// Gets or sets the current message filter generated from the Ui state
        /// </summary>
        public IMessageFilter CurrentFilter { get; set; }

        /// <summary>
        /// Gets the command to generate the filter
        /// </summary>
        public ICommand GenerateFilterCommand
        {
            get { return this.generateFilterCommand; }
        }

        /// <summary>
        /// Gets the command to clear the filter
        /// </summary>
        public ICommand ClearFilterCommand
        {
            get { return this.clearFilterCommand; }
        }

        /// <summary>
        /// Gets  the command to read settings
        /// </summary>
        public ICommand ReadSettingsCommand
        {
            get { return this.readSettingsCommand; }
        }

        /// <summary>
        /// Gets  the command to write settings
        /// </summary>
        public ICommand WriteSettingsCommand
        {
            get { return this.writeSettingsCommand; }
        }

        /// <summary>
        /// Gets the command to set up selection lists
        /// </summary>
        public ICommand SetSelectionListsCommand
        {
            get { return this.setSelectionListsCommand; }
        }

        #endregion
    }
}
